import { Toast } from 'vant';
import ajax from './ajax';

const ajaxVant = {};

function handleResponse(response) {
  if (response.status !== 200) {
    /*
    Toast('Status: ' + response.status + ', Message: ' + response.message);
    */
    Toast(response.message);
    return false;
  } else if (response.data.code !== 0) {
    Toast(response.data.msg);
    return false;
  } else {
    return response.data;
  }
}

function handleCatch(error) {
  /*
  Toast('Status: ' + error.status + ', Message: ' + error.message);
  */
  Toast(error.message);
  return false;
};

ajaxVant.get = function(url) {
  return ajax.get(url).then(handleResponse).catch(handleCatch);
};

ajaxVant.post = function(url, param = {}) {
  return ajax.post(url, param).then(handleResponse).catch(handleCatch);
};

ajaxVant.put = function(url, param = {}) {
  return ajax.put(url, param).then(handleResponse).catch(handleCatch);
};

ajaxVant.delete = function(url) {
  return ajax.delete(url).then(handleResponse).catch(handleCatch);
};

export default ajaxVant;
